home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_unix.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  170 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_unix.c,v 1.4 92/10/21 13:42:21 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library UNIX-specific Routines.
  31.  */
  32. #include "tiffiop.h"
  33.  
  34. static int
  35. DECLARE3(_tiffReadProc, void*, fd, char*, buf, u_long, size)
  36. {
  37.     return (read((int) fd, buf, size));
  38. }
  39.  
  40. static int
  41. DECLARE3(_tiffWriteProc, void*, fd, char*, buf, u_long, size)
  42. {
  43.     return (write((int) fd, buf, size));
  44. }
  45.  
  46. static long
  47. DECLARE3(_tiffSeekProc, void*, fd, long, off, int, whence)
  48. {
  49.     return (lseek((int) fd, off, whence));
  50. }
  51.  
  52. static int
  53. DECLARE1(_tiffCloseProc, void*, fd)
  54. {
  55.     return (close((int) fd));
  56. }
  57.  
  58. #include <sys/stat.h>
  59.  
  60. static long
  61. DECLARE1(_tiffSizeProc, void*, fd)
  62. {
  63.     struct stat sb;
  64.     return (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
  65. }
  66.  
  67. #ifdef MMAP_SUPPORT
  68. #include <sys/mman.h>
  69.  
  70. static int
  71. DECLARE3(_tiffMapProc, void*, fd, char**, pbase, long*, psize)
  72. {
  73.     long size = _tiffSizeProc(fd);
  74.     if (size != -1) {
  75.         *pbase = (char*)
  76.             mmap(0, size, PROT_READ, MAP_SHARED, (int) fd, 0);
  77.         if (*pbase != (char *)-1) {
  78.             *psize = size;
  79.             return (1);
  80.         }
  81.     }
  82.     return (0);
  83. }
  84.  
  85. static void
  86. DECLARE3(_tiffUnmapProc, void*, fd, char*, base, long, size)
  87. {
  88.     (void) munmap(base, size);
  89. }
  90. #else /* !MMAP_SUPPORT */
  91. static int
  92. DECLARE3(_tiffMapProc, void*, fd, char**, pbase, long*, psize)
  93. {
  94.     return (0);
  95. }
  96.  
  97. static void
  98. DECLARE3(_tiffUnmapProc, void*, fd, char*, base, long, size)
  99. {
  100. }
  101. #endif /* !MMAP_SUPPORT */
  102.  
  103. /*
  104.  * Open a TIFF file descriptor for read/writing.
  105.  */
  106. TIFF*
  107. DECLARE3(TIFFFdOpen, int, fd, const char*, name, const char*, mode)
  108. {
  109.     TIFF *tif;
  110.  
  111.     tif = TIFFClientOpen(name, mode,
  112.         (void*) fd,
  113.         _tiffReadProc, _tiffWriteProc,
  114.         _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  115.         _tiffMapProc, _tiffUnmapProc);
  116.     if (tif)
  117.         tif->tif_fd = fd;
  118.     return (tif);
  119. }
  120.  
  121. /*
  122.  * Open a TIFF file for read/writing.
  123.  */
  124. TIFF*
  125. DECLARE2(TIFFOpen, const char*, name, const char*, mode)
  126. {
  127.     static const char module[] = "TIFFOpen";
  128.     int m, fd;
  129.  
  130.     m = _TIFFgetMode(mode, module);
  131.     if (m == -1)
  132.         return ((TIFF *)0);
  133.     fd = open(name, m, 0666);
  134.     if (fd < 0) {
  135.         TIFFError(module, "%s: Cannot open", name);
  136.         return ((TIFF *)0);
  137.     }
  138.     return (TIFFFdOpen(fd, name, mode));
  139. }
  140.  
  141. #if defined(__MACH__) || defined(THINK_C)
  142. extern    void *malloc(size_t size);
  143. extern    void *realloc(void *ptr, size_t size);
  144. #else /* !__MACH__ && !THINK_C */
  145. #if defined(_IBMR2)
  146. #include <stdlib.h>
  147. #else /* !_IBMR2 */
  148. extern    char *malloc();
  149. extern    char *realloc();
  150. #endif /* _IBMR2 */
  151. #endif /* !__MACH__ */
  152.  
  153. void *
  154. DECLARE1(_TIFFmalloc, size_t, s)
  155. {
  156.     return (malloc(s));
  157. }
  158.  
  159. void
  160. DECLARE1(_TIFFfree, void*, p)
  161. {
  162.     free(p);
  163. }
  164.  
  165. void *
  166. DECLARE2(_TIFFrealloc, void*, p, size_t, s)
  167. {
  168.     return (realloc(p, s));
  169. }
  170.